// ----------------------------------------------------------------------------
// chunk.cpp
// ----------------------------------------------------------------------------
#include <fw/base/serialize/chunk.h>

namespace fw
{
	
	Chunk::~Chunk()
	{
		Chunk* child = m_child;
		while(child)
		{
			Chunk* siblingBlock = child->m_sibling;
			delete child;
			child = siblingBlock;
		}
	}
	
	Chunk* Chunk::AddChild(Chunk* chunk)
	{
		if(m_child == nullptr)
		{
			m_child = chunk;
		}
		else
		{
			// 最後の子供ノードの後続ノードとして追加する.
			Chunk* child = m_child;
			while(child->m_sibling)
			{
				child = child->m_sibling;
			}
			child->m_sibling = chunk;
		}
		m_childCount++;
		return chunk;
	}
	
	const Chunk* Chunk::FindChild(uint32_t id) const
	{
		Chunk* child = m_child;
		while(child)
		{
			if(child->m_id == id)
			{
				return child;
			}
			child = child->m_sibling;
		}
		return nullptr;
	}
	
} // namespace fw 

